class Solution
{
public:
    int findMinimumTime(vector<vector<int>> &tasks)
    {
        unordered_set<int> runTimes;

        // 按end从小到大排序
        sort(tasks.begin(), tasks.end(), [](auto &t1, auto &t2)
             {
            if(t1[1] == t2[1])
            {
                return t1[0] < t2[0];
            }
            return t1[1] < t2[1]; });

        for (auto &task : tasks)
        {
            int duration = task[2];
            int start = task[0];
            int end = task[1];

            // 减去已经使用的时间点
            for (int t = start; t <= end && duration > 0; ++t)
            {
                if (runTimes.count(t))
                {
                    --duration;
                }
            }

            // 从后往前分配时间点
            for (int t = end; t >= start && duration > 0; --t)
            {
                if (!runTimes.count(t))
                {
                    runTimes.insert(t);
                    --duration;
                }
            }
        }
        return runTimes.size();
    }
};